package com.hs.myshell.ssh;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.hs.myshell.edit.TerminalEditor;

import net.schmizz.sshj.DefaultConfig;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.common.LoggerFactory;
import net.schmizz.sshj.common.StreamCopier;
import net.schmizz.sshj.connection.ConnectionException;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
import net.schmizz.sshj.transport.TransportException;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;


/**
 * sshj实现
 * @author Administrator
 *
 */
public class ISSHClientSSHJ implements ISSHClient {
	
	private String hostname = "120.78.13.172";
	private Integer port = 22;
    private String username = "root";
    private String password = "X5f#PYpzb12312@#@#11sdf@#$";
    
    private Session session;
    
    private net.schmizz.sshj.SSHClient ssh;

    
	public ISSHClientSSHJ(String hostname, Integer port, String username, String password) {
		super();
		this.hostname = hostname;
		this.port = port;
		this.username = username;
		this.password = password;
	}

	@Override
	public void login() {
		ssh = new net.schmizz.sshj.SSHClient();
		ssh.addHostKeyVerifier(new PromiscuousVerifier());
        try {
			ssh.connect(hostname, 22);
			ssh.authPassword("root", password);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void logout() {
		// TODO Auto-generated method stub

	}

	@Override
	public void putFile(String localPath, String localFile, String remotePath) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public String sendCmd(String command, TerminalEditor screen) {
		try {
			final Session session = ssh.startSession();
			Command cmd = session.exec(command);
//			ByteArrayOutputStream readFully = IOUtils.readFully(cmd.getInputStream());
//			String string = readFully.toString();
//			screen.appendOutPut(string);
		} catch (ConnectionException e) {
			e.printStackTrace();
		} catch (TransportException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public String shell(TerminalEditor openEditor){
		try {
            final Session session = ssh.startSession();
            try {

                session.allocateDefaultPTY();

                final Session.Shell shell = session.startShell();
                openEditor.setOriginInputStream(shell.getInputStream());
                System.out.println("set inputStream: "+openEditor.getOriginInputStream());
                openEditor.setOriginOutputStream(shell.getOutputStream());
//                new StreamCopier(shell.getInputStream(), openEditor.getOutPut(), LoggerFactory.DEFAULT)
//                        .bufSize(shell.getLocalMaxPacketSize())
//                        .spawn("stdout");

//                new StreamCopier(shell.getErrorStream(), System.err, LoggerFactory.DEFAULT)
//                        .bufSize(shell.getLocalMaxPacketSize())
//                        .spawn("stderr");
//
//                new StreamCopier(System.in, shell.getOutputStream(), LoggerFactory.DEFAULT)
//                        .bufSize(shell.getRemoteMaxPacketSize())
//                        .copy();

            } catch (IOException e) {
				e.printStackTrace();
			} finally {
//                try {
//					session.close();
//				} catch (TransportException | ConnectionException e) {
//					e.printStackTrace();
//				}
            }

        } catch (ConnectionException | TransportException e1) {
			e1.printStackTrace();
		} finally {
//            try {
//				ssh.disconnect();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
        }
		return null;
	}

}
